/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.listener;

import com.minierp.dao.IdGenerator;
import com.minierp.dao.SmsCountDao;
import com.minierp.dao.SmsTaskDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.SmsTask;
import com.minierp.service.SmsService;
import java.util.List;
import org.springframework.web.context.WebApplicationContext;

class SmsSendThread extends Thread {
	private boolean runFlag = true;
	private SmsTaskDao smsTaskDao;
	private SmsCountDao smsCountDao;
	private IdGenerator idGenerator;
	private SmsService smsService;

	SmsSendThread(WebApplicationContext wac) {
		this.smsTaskDao = (SmsTaskDao) wac.getBean("smsTaskDao");
		this.smsCountDao = (SmsCountDao) wac.getBean("smsCountDao");
		this.idGenerator = (IdGenerator) wac.getBean("idGenerator");
		this.smsService = (SmsService) wac.getBean("smsService");
	}

	public void setRunFlag(boolean runFlag) {
		this.runFlag = runFlag;
	}

	public void run() {
		String smsClientType = EncCommonHelper.getProperty("smsClientType");
		if (smsClientType != null && !smsClientType.equals("")) {
			EncCommonHelper.debug("************sms send:thread runing...");

			while (this.runFlag) {
				try {
					boolean e = false;
					List taskList = this.smsTaskDao.getList(20);
					if (taskList.size() > 0) {
						e = true;
					}

					while (true) {
						if (!e) {
							PageHelper.clearSmsClient();
							break;
						}

						for (int i = 0; i < taskList.size(); ++i) {
							SmsTask st = (SmsTask) taskList.get(i);
							int s = -1;

							for (int j = 0; j < 3; ++j) {
								s = PageHelper.smsSend(st.getReceiverCode(),
										st.getContent(), st.getPlanTime());
								EncCommonHelper
										.debug("************sms send:time" + j
												+ " " + s);
								if (s == 0) {
									break;
								}

								try {
									Thread.sleep(1000L);
								} catch (Exception arg9) {
									;
								}
							}

							this.smsService.txSmsRecord(st, s, this.smsTaskDao,
									this.smsCountDao, this.idGenerator);
						}

						taskList = this.smsTaskDao.getList(20);
						if (taskList.size() > 0) {
							e = true;
						} else {
							e = false;
						}
					}
				} catch (Exception arg10) {
					arg10.printStackTrace();
				}

				try {
					Thread.sleep(120000L);
				} catch (Exception arg8) {
					;
				}
			}
		}

	}
}